package lockAndCondition;

import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;


public class ConditionDemo1 {

	public static Lock lock=new ReentrantLock();
	public static Condition condition1=lock.newCondition();
	public static Condition condition2=lock.newCondition();
	public static Condition condition3=lock.newCondition();
	static int  runId=0;
	
	public static void main(String[] args){
		new Thread(new Runnable() {
			public void run() {
				// TODO Auto-generated method stub
				for(int j=0;j<50;j++){
					try {
						main();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
				
		}).start();
		new Thread(new Runnable() {
			public void run() {
				// TODO Auto-generated method stub
				for(int j=0;j<50;j++){
					sub();
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			public void run() {
				// TODO Auto-generated method stub
				for(int j=0;j<50;j++){
						try {
							sub2();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
			}
		}).start();
	}
	
	public   static void  sub() {
		lock.lock();
		while(runId %3 !=0){
			try {
				condition1.await();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			int i=0;
			while(i<10){
				System.out.println("sub 1 = "+i++);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			runId++;
			condition2.signal();
			lock.unlock();
			try {
				Thread.currentThread().sleep(new Random().nextInt(50));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public   static void  sub2() throws InterruptedException{
		lock.lock();
		while(runId %3 !=1){
			try {
				condition2.await();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			int i=0;
			while(i<10){
				System.out.println("sub 2 = "+i++);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			runId++;
			condition3.signal();
			lock.unlock();
			Thread.currentThread().sleep(new Random().nextInt(50));
		}
		
}
	
	public  static void main() throws InterruptedException{
		lock.lock();
		while(runId %3 !=2){
			try {
				condition3.await();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			int i=0;
			while(i<10){
				System.out.println("main  = "+i++);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			runId++;
			condition1.signal();
			lock.unlock();
			Thread.currentThread().sleep(new Random().nextInt(50));
		}
		
		
	}
}
